from django.shortcuts import render
from django.views.decorators.http import last_modified
from rest_framework import viewsets
from rest_framework.response import Response
from rest_framework.decorators import action
from .serializers import StudentSerializer, ClassSerializer
from .models import Students,Classes
from rest_framework.status import HTTP_201_CREATED,HTTP_404_NOT_FOUND,HTTP_204_NO_CONTENT

# Create your views here.


# class StudentViewSet(viewsets.ViewSet):
#
#     # 查看所有学生
#     def list(self, request):
#         # 获取所有的学生信息
#         students = Students.objects.filter(is_delete=False) # 查询集 [(模型对象),(模型对象)]
#         # 通过序列化器吧 queryset转换成 json格式数据  序列化
#         # 吧要转换的数据 传入 序列化器作为参数  如果是多个学生 many = True
#         serialize = StudentSerializer(students, many=True)
#         # serialize.data  里面保存的就是json格式数据
#         # 通过rest_framework通过的Response 返回 json格式数据
#         return Response(serialize.data)
#
#     # 添加学生
#     def create(self, request):
#         # 从请求里面得到学生数据 （json） request.data
#         # 通过序列化器进行 反序列化
#         #  serialize==>实例对象
#         serialize = StudentSerializer(data=request.data)
#         #数据校验
#         serialize.is_valid(raise_exception=True)
#         #数据持久化保存
#         serialize.save()
#         #返回响应
#         return Response(serialize.data,status=HTTP_201_CREATED)
#
#     def retrieve(self, request, pk=None):
#         # 获取指定学生数据
#         try:
#             student = Students.objects.filter(is_delete=False).get(id=pk)
#         except Students.DoesNotExist:
#             return Response(status=HTTP_404_NOT_FOUND)
#
#         # 进行序列化器  单条数据  many=False
#         serlializer = StudentSerializer(student)
#         return Response(serlializer.data)
#
#     def update(self, request, pk=None):
#         # 获取指定学生数据
#         try:
#             student = Students.objects.filter(is_delete=False).get(id=pk)
#         except Students.DoesNotExist:
#             return Response(status=HTTP_404_NOT_FOUND)
#
#         # 获取前端传入的json格式数据  新数据 request.data
#         # 进行 反序列化
#         serlializer = StudentSerializer(instance=student, data=request.data)
#         #数据校验
#         serlializer.is_valid(raise_exception=True)
#         serlializer.save()
#
#         return Response(serlializer.data,status=HTTP_201_CREATED)
#
#     def destroy(self,request,pk=None):
#         # 获取指定学生数据
#         try:
#             student = Students.objects.filter(is_delete=False).get(id=pk)
#         except Students.DoesNotExist:
#             return Response(status=HTTP_404_NOT_FOUND)
#
#         # 调用删除方法  ==》 调用修改方法
#         student.delete()
#
#         #返回状态码
#         return Response(status=HTTP_204_NO_CONTENT)

class StudentViewSet(viewsets.ModelViewSet):
    #指定查询数据
    queryset = Students.objects.filter(is_delete=False)
    #指定序列化器
    serializer_class = StudentSerializer

class ClassesViewSet(viewsets.ModelViewSet):
    # 指定查询数据
    queryset = Classes.objects.filter(is_delete=False)
    # 指定序列化器
    serializer_class = ClassSerializer

    # 查看最新的班级 get
    # 127.0.0.1:8000/drfstudy/classes/last_classes --不用在urls.py文件注册路由，action已经注册
    # False 就是不带参数

    @action(methods=["get"], detail=False)
    def last_classes(self,request):
        #模型对象
        #last_class = Classes.objects.filter(is_delete=False).last() # 在这里不能直接使用上面的queryset，因为在类里面，要使用的话要self.名字
        # self.get_queryset() == Classes.objects.filter(is_delete=False) --> 上面定义的
        last_classes = self.get_queryset().last()


        #序列化
        #serializer = ClassSerializer(last_class)
        #  self.get_serializer(last_class) == ClassSerializer(last_class) --> 父类定义的了
        serializer = self.get_serializer(last_classes)
        #响应数据
        return Response(serializer.data)


